/* 
 * @(#)ServletProcessor2.java 2012-5-2
 * Copyright (c) 2012
 * Dark-J (Zh)Co., Ltd.
 * Dark-J R&D,BeiJing
 *
 * All Rights Reserved
 * 
 * Revision History:
 * 								Modification	 Traking
 * Author (Email ID)			Date			 Number				Description
 * ----------------------------------------------------------------------------------
 * yongchong.bai				2012-5-2		 					Initial Version
 */

package com.jworktogether.application2;

import java.net.URL; 
import java.net.URLClassLoader; 
import java.net.URLStreamHandler; 
import java.io.File; 
 
import java.io.IOException; 
import javax.servlet.Servlet; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 

/**
 * @author baiyc
 * @version 1.0
 */
public class ServletProcessor2 { 
 
   public void process(Request request, Response response) throws Exception { 
     String uri = request.getUri(); 
     String servletName = uri.substring(uri.lastIndexOf("/") + 1); 
     URLClassLoader loader = null; 
     try { 
       // create a URLClassLoader 
       URL[] urls = new URL[1]; 
       URLStreamHandler streamHandler = null; 
       File classPath = new File(Constants.WEB_ROOT); 
       // the forming of repository is taken from the 
       // createClassLoader method in 
       // org.apache.catalina.startup.ClassLoaderFactory 
       String repository = 
        (new URL("file", null, classPath.getCanonicalPath() + 
        File.separator)).toString() ; 
       // the code for forming the URL is taken from 
       // the addRepository method in 
       // org.apache.catalina.loader.StandardClassLoader. 
       urls[0] = new URL(null, repository, streamHandler); 
       loader = new URLClassLoader(urls); 
     } 
     catch (IOException e) {        System.out.println(e.toString() ); 
     } 
     Class myClass = null; 
     try { 
       myClass = loader.loadClass(servletName); 
     } 
     catch (ClassNotFoundException e) { 
       System.out.println(e.toString()); 
     } 
 
     Servlet servlet = null; 
     RequestFacade requestFacade = new RequestFacade(request); 
     ResponseFacade responseFacade = new ResponseFacade(response); 
     try { 
       servlet = (Servlet) myClass.newInstance(); 
       servlet.service((ServletRequest) requestFacade, 
         (ServletResponse) responseFacade); 
     }catch(Exception e){
    	 throw e;
     }
   } 
} 

